<?php
class Api_Lib_Rsp_AcmUser {

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Acm_User');
        $this->_modelRole = FLEA::getSingleton('Model_Acm_Role');
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function update($params,& $service){
        if(!$params['userName'] || !$params['roleName']){
            $service->send_user_error('userName,roleName required');
        }

        //检查是否存在
        $user = $this->_modelExample->find(array('userName'=>$params['userName']));

        //存在多部门，多部门用,隔开
        //查找是否存在角色表，不存在需要处理
        $roles = explode(',' ,$params['roleName']);
        $role = array();
        foreach ($roles as & $r) {
            $tmp = $this->_modelRole->find(array('roleName'=>$r));
            if(!$tmp){
                $_role = array(
                    'roleName'=>$r,
                );
                $roleid = $this->_modelRole->save($_role);
            }else{
                $roleid = $tmp['id'];
            }

            $role[] = $roleid;
        }

        //处理密码
        $params['passwd'] = $params['passwd'] ? $params['passwd'] : '123456';

        // 处理业务员
        if($params['Traders']){
            $traders = join(',' ,$params['Traders']);
            $sql = "SELECT id from jichu_employ where linkId in ({$traders})";
            $tmp = $this->_modelExample->findBySql($sql);
            $params['Traders'] = array_col_values($tmp ,'id');
        }

        $_data = array(
            'id'           => $user['id']+0,
            'userName'     => $params['userName'],
            'realName'     => $params['realName'],
            'passwd'       => _md5($params['passwd']),
            'phone'        => $params['phone'],
            'shenfenzheng' => $params['shenfenzheng'],
            'roles'        => $role,
            'traders'      => $params['Traders'],
        );
        // dump2file($_data);

        //数据保存
        $result = $this->_modelExample->save($_data);
        $user['id'] = $user['id'] ? $user['id'] : $result;

        return array('success'=>$result ? true :false ,'msg'=>'完成','data'=>array('id'=>$user['id'],'passwd'=>$params['passwd']));
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function delete($params,& $service){
        if(!$params['userName']){
            $service->send_user_error('userName required');
        }

        //检查是否存在
        $user = $this->_modelExample->find(array('userName'=>$params['userName']));

        //删除
        $result = $this->_modelExample->removeByPkv($user['id']);

        return array('success'=>$result ,'msg'=>'完成');
    }

    /**
     * 获取客户列表
     * Time：2019/11/07 15:09:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getList($params,& $service){
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $condition = array();
        if($params['userName']!='') {
            $condition[] = array('userName',"%{$params['userName']}%",'=');
        }

        // $this->_modelExample->clearLinks();
        FLEA::loadClass('TMIS_Pager');
        $pager = & new TMIS_Pager($this->_modelExample,$condition,'id desc',$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        //把需要的数据带出来
        $rows = array();
        foreach ($rowset as $key => & $v) {
            $rows[] = array(
                'id'       => $v['id'],
                'userName' => $v['userName'],
                'realName' => strval($v['realName']),
                'email'    => strval($v['email']),
                'phone'    => strval($v['phone']),
                'roles'    => (array)$v['roles'],
            );
        }

        return array('row'=>$rows,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rows));
    }

    /**
     * 获取权限说明配置表
     * Time：2020/03/31 10:52:40
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function func2role(){
        //获取菜单配置和权限配置
        $modelFunc = FLEA::getSingleton('Model_Acm_Func');
        $ret = $modelFunc->getAuthTreeData(array(
            'children'=>$modelFunc->menu
        ));

        //去掉多余的，仅提供样品间这块
        $this->funcFormat($ret);

        return $ret;

    }

    //把数据格式化
    function funcFormat(& $funcs = array()){
        foreach ($funcs as $key => & $func) {
            if(intval($func['id']) > 10){
                unset($funcs[$key]);
            }
            unset($func['value']);
            if($func['children']){
                $this->funcFormat($func['children']);
            }
        }
    }
}
?>